#include <afx.h>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/features2d/features2d.hpp>
#include "SIFTDll.h"

/*
#pragma comment(lib, "opencv_imgproc231.lib")
#pragma comment(lib, "opencv_core231.lib")
#pragma comment(lib, "opencv_highgui231.lib")
#pragma comment(lib, "SIFTDll.lib")
*/

// if (readInitData)
//    //do incremental(online) learning

// if (!readInitData)
//    //do offline learning

// Para: trainDataFile 
// //File format: sample_num class_num feature_dimention_num 
// //File format: label_of_sample1(0 0 1 0 ...) feature_of_sample1(0.01 0.014 ...)	 // feature should be normalized by N1 or N2
void trainELM(LPCTSTR trainDataFile, bool readInitData);

void trainELM2(LPCTSTR trainDataFile, LPCTSTR trainTagFile, bool readInitData);

// Para: testDataFile
// rstFileName ouput_file_path
// testDataFile format: the same as the trainDataFile, but the label_domain can be NULL (all zero, then statistical result is invalid)
void testELM(LPCTSTR testFileName, LPCTSTR rstFileName);

void testELM2(LPCTSTR testFileName, LPCTSTR rstTagName, LPCTSTR rstFN);


// Para: imgNameList pathes of the image files
// labelFN labels of the images (0 0 1 1 1 2 2 ...)
// dicFN path of the visual dictionary file
// rstFN file name of the extracted feature and lable file

void updataImages(CString fname);

void formatImage2Feature(LPCTSTR imgNameList, LPCTSTR labelFN, LPCTSTR dicFN, int classNum, LPCTSTR rstFN, int mode);

void formatImage2SpatialFeature3(LPCTSTR imgNameList, LPCTSTR dicFN, LPCTSTR rstFN, int mode);

void formatPerImage2SpatialFeature(LPCTSTR imgNameList, LPCTSTR dicFN, LPCTSTR rstDir, int mode);

void formatImage2SpatialFeature2(LPCTSTR imgNameList, LPCTSTR dicFN, LPCTSTR rstFN);

void collectSpatialFeature(CString fn, CString rstFN);

void loadSPCode3(CString fn, float **&mat, int &row);

float computeSPCodeSim2(float *data1, float * data2);

void loadLDCode2(CString fn, float **&mat, int &row);

float computeLDCodeSim(float *data1, float * data2);

void computeKernel(CString dataFN, CString rstFN, int mode);

void computeSimMat(int mode, CString testFN, CString trainFN, CString rstFN);

void formatImage2SpatialFeature(LPCTSTR imgNameList, LPCTSTR dicFN, LPCTSTR rstFN, int mode);

void getFeature4Img(CString tokenFN, CString imageFN, CString rstFN);

void getPHOGFeature(CString fname, CString rstFN, bool ori_type, bool bin_type);

void getFeature4Region(CString segFN, CString tokenFN, CString regFN, CString rstFN);

void selFormat4LD(CString corpusFN, CString rstFN);

void selFormat4SP(CString corpusFN, CString rstFN);

void selFormat4SP_selected(vector<CString> &corpusFNs, CString indFN, CString rstFN);

void selFormat4SP2(vector<CString> fnames, CString rstFN, int size);

void getSelSiftHist(CString siftCorpusFN, CString rstFN);

void sortKNN(CString kernelMatrix, CString rstFN);

void resaveFileList(CString srcFN, CString rstDir, CString fix, CString rstFN);

// mode 1 dense-sift
// mode 2 sift
// 3 CIE-lab Gaussian
// 4 CIE-lab l channel LoG
// 5 CIE-lab l channel D1oG 
void createVisualCodeBook(LPCTSTR imgListFN, CString rstFN, int k, int mode, bool trainVaculary);

void createVisualCodeBook2(LPCTSTR imgListFN, int k);


// combined prototypes
void createProtoCodeBook(LPCTSTR imgListFN, vector<CString> dicFNs, int k);

void getProtoFeatures(LPCTSTR imgListFN, vector<CString> dicFNs, CString protoDicFN, CString rstDir);

void findGroundTruth(CString imgListFN, int classId, CvScalar temp, CString rstDir);

void getGroundTruthMat(CString imgListFN, int classId, CvScalar temp, CString rstFN);

void labelRegions(CString segFN, CString gtFN, CString rstFN);

void getProtoFeaturesToMat(LPCTSTR imgListFN, vector<CString> dicFNs, CString protoDicFN, CString rstFN);

void getH(CString xFN, CString rstFN);

void getC(CString xFN, CString gFN, CString rstFN);

void test(CString HFN, CString CFN, CString rstFN);

void getBoxFea(CString corpusFN, CString boxFN, CString rstDir);

void getAllHist(vector<CString> &fnames, int *bounds);

void getMeanMat(vector<CString> &fnames, CString rstFN, bool sym);

void getMeanMat2(vector<CString> &fnames, CString rstFN);

void getMultiLabels(CString srcFN, CString rstFN, vector<int> &inds);

// grouping with plsa
void plsaGrouping(CString dataFN, CString labelFN, CString rstFN);

void plsaGrouping2(CString dataFN, CString labelFN, CString rstFN);

void findStates(CString kernelFN, CString labelFN, CString rstFN, int cate, int k);

void findStates2(vector<CString> &kernelFNs, CString labelFN, CString rstFN, int cate, int k);

void plsaGrouping3(CString dir, CString imageFNames, CString dataFN, CString labelFN, CString rstFN, int cate);

void getSelSiftHist2(vector<CString> &siftCorpusFNs, CString rstFN, int code_size);

void splitMat(CString srcFN, int bound, CString rstFN);

void testGlobalKernelKmeans(CString kernelFN, int k, int max_iter);

void loadSURFFeature(CString fname);

void estimateNN(CString matFN, CString map, CString rstFN, int k);

void estimateNN3(vector<CString> &kernelFNs, CString labelFN, CString tLabelFN, float *beta, CString rstFN, int k, int numClass, bool sym,  bool loadBeta);